package com.signway.utils;

import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 常规签名算法类工具
 */
public class SignatureTools {

    /**
     * 获取文件BYTE格式的签名数据
     *
     * @param signature
     * @return
     */
    public static byte[] getFileSigByte(String signature) {
        byte[] bSig = new byte[16];
        if ((signature == null) || (signature.length() != 32)) {
            signature = "00000000000000000000000000000000";
        }
        // Log.v(TAG, "getFileSigByte signature = " + signature);
        bSig = CharTools.stringToHex(signature);
        return bSig;
    }

    public static byte[] getMD5(byte[] buf) {
        return getMD5(buf, 0, buf.length);
    }

    public static byte[] getMD5(byte[] buf, int offset, int length) {
        MessageDigest algorithm = null;
        try {
            algorithm = MessageDigest.getInstance("MD5");
            algorithm.update(buf, offset, length);
            return algorithm.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] getMD5(String filename, int lessLen) {
        final int bufferSize = 256 * 1024;
        FileInputStream fis = null;
        DigestInputStream digestInputStream = null;

        try {
            // 拿到一个MD5转换器（同样，这里可以换成SHA1）
            MessageDigest algorithm = MessageDigest.getInstance("MD5");

            // 使用DigestInputStream
            fis = new FileInputStream(filename);
            digestInputStream = new DigestInputStream(fis, algorithm);
            int fileLen = digestInputStream.available();
            if (fileLen <= lessLen) {
                return null;
            }

            fileLen -= lessLen;

            // read的过程中进行MD5处理，直到读完文件
            byte[] buffer = new byte[bufferSize];
            int bufSize = (fileLen > bufferSize) ? bufferSize : fileLen;
            int readLen = 0;
            while ((readLen = digestInputStream.read(buffer, 0, bufSize)) > 0) {
                fileLen -= readLen;
                bufSize = (fileLen > bufferSize) ? bufferSize : fileLen;
            }

            // 获取最终的MessageDigest
            algorithm = digestInputStream.getMessageDigest();
            return algorithm.digest();
        } catch (Exception e) {

        } finally {
            if (digestInputStream != null) {
                try {
                    digestInputStream.close();
                } catch (Exception e) {

                }
            }

            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {

                }
            }
        }

        return null;
    }

    public static String getMD5(String string) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getSignature(byte[] bytes) {
        if (bytes == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            int value = bytes[i] & 0xff;
            if (value < 0x10) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(value));
        }
        return sb.substring(0, sb.length());
    }
}
